package com.ithao.controller;

import com.ithao.common.GlobalConst;
import com.ithao.common.QuestionConst;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.entity.Result;
import com.ithao.pojo.Tag;
import com.ithao.pojo.User;
import com.ithao.service.TagService;
import com.ithao.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @description ：学科标签控制器
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/tag")
public class TagController {

	@Autowired
	private TagService tagService;

	/**
	 * 添加学科标签
	 */
	@RequestMapping("add")
	public Result addTag (@RequestBody Tag tag, HttpServletRequest request){
		try{
			// 初始化用户ID，状态和创建时间
			User user = (User) request.getSession().getAttribute(GlobalConst.SESSION_KEY_USER);
			tag.setUserId( user.getId());
			if (user != null) {
				tag.setUserId(user.getId());
			} else {
				// 调试时，默认是管理员
				tag.setUserId(1);
			}
			tag.setStatus(QuestionConst.TagStatus.ENABLE.ordinal());
			tag.setCreateDate(DateUtils.parseDate2String(new Date()));
			tagService.addTag(tag);
			return new Result(true,"添加学科标签成功");
		}catch(RuntimeException e){
			log.error("addTag",e);
			return new Result(false,"添加学科标签失败，"+e.getMessage());
		}
	}

	/**
	 * 分页获取学科标签列表
	 */
	@RequestMapping("findListByPage")
	public Result findListByPage (@RequestBody QueryPageBean queryPageBean){
		if (queryPageBean == null){
			queryPageBean = new QueryPageBean();
			queryPageBean.setCurrentPage(1);
			queryPageBean.setPageSize(10);
		}
		// 获取当前用户的学科方向
		//pageBean.getQueryParams().put("courseId",request.getParameter("courseId"));
		log.info("questionList queryPageBean:{}",queryPageBean);
		PageResult pageResult = tagService.findListByPage(queryPageBean);
		return new Result(true,"获取学科标签列表成功",pageResult);
	}

	/**
	 * 删除学科标签
	 */
	@RequestMapping("/delete")
	public Result deleteTag (Integer tagId){
		try{
			log.info("deleteTag id:{}",tagId);
			tagService.deleteTag(tagId);
			return new Result(true,"删除学科标签成功");
		}catch(RuntimeException e){
			log.error("deleteTag",e);
			return new Result(false,"删除学科标签失败,"+e.getMessage());
		}
	}

	@PostMapping("/update")
	public Result update(@RequestBody Tag tag) {
		log.info("[更新目录] tag:{}",tag);
		tagService.update(tag);
		return new Result(true,"更新成功");
	}
	/**
	 * 标签的启用禁用
	 */
	@RequestMapping("/updateStatus")
	public Result updateStatus(@RequestBody Map map) {
		if (map == null || map.size() == 0) {
			throw new RuntimeException("参数为空");
		}
		Integer id = (Integer) map.get("id");
		Integer status = (Integer) map.get("status");
		log.info("[标签的禁用启用 ] catalogId:{}   status:{}", id, status);
		if (status == 0) {
			tagService.updateStatus(id, 1);
			return new Result(true, "标签启动成功");
		} else  {
			tagService.updateStatus(id, 0);
			return new Result(true, "标签禁用成功");
		}
	}
}
